package com.example.ggkt.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ggkt.common.BaseException;
import com.example.ggkt.user.domain.Student;
import com.example.ggkt.user.mapper.StudentMapper;
import com.example.ggkt.user.service.StudentService;
import com.ggkt.common.entity.QueryFilter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 * 通过mybatisplus自带分页实现分页
 * @author ggkt
 * @since 2021-05-24
 */
@RestController
@RequestMapping("/student")
@Api(tags = "学生管理")
public class StudentController {
    @Autowired
    StudentService studentService;
    @Autowired
    StudentMapper studentMapper;

    @ApiOperation("列表方法")
    @PostMapping("/list")
    public List<Student> list(@RequestBody QueryFilter<Student> entity){
         Page<Student> page = new Page<>(entity.getPageNo(), entity.getPageSize());
         QueryWrapper<Student> queryWrapp=entity.toWrapper();
         queryWrapp.ge("age",20);
         //Wrappers.<Student>lambdaQuery().ge(Student::getAge, entity.getJson().getInteger("age"));

         Page<Student> userIPage = studentMapper.selectPage(page,queryWrapp);
         return userIPage.getRecords();
    }


    @ApiOperation("创建方法")
    @PostMapping("/create")
    public boolean create(@RequestBody Student entity){
        return studentService.saveOrUpdate(entity);
    }

    @ApiOperation("查看方法")
    @GetMapping("/view/{id}")
    public Student view(@PathVariable Integer id){
        if(id==null){
            throw new BaseException("111");
//            throw new RuntimeException("111");
        }
        return studentService.getById(id);
    }

    @ApiOperation("删除方法")
    @PostMapping("delete")
    public boolean delete(@RequestBody Student entity){
        if (entity==null || entity.getId()==null){
            System.out.println("参数错误");
            return false;
        }else{
            System.out.println("the id is:"+entity.getId());
        }
        return  studentService.removeById(entity.getId());
    }

}

